package com.zimi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zimi.model.SysUser;
import com.zimi.model.SysUserInfo;
import com.zimi.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("auth")
public class AuthApi {

    private final AuthService authService;

    @Autowired
    public AuthApi(AuthService authService) {
        this.authService = authService;
    }

    @PostMapping("app")
    public Object app() {
        return null;
    }

    @PostMapping("get/user")
    public Object getUser(String openId) {
        return new SysUser().selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getOpenId, openId));
    }

    @PostMapping("reg")
    public Object reg(SysUser sysUser, String icon, String birthday) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        sysUser.insert();
        SysUserInfo sysUserInfo = new SysUserInfo();
        sysUserInfo.setSysUserId(sysUser.getId());
        sysUserInfo.setBirthday(format.parse(birthday));
        sysUserInfo.setIcon(icon);
        sysUserInfo.insert();
        return sysUser.getId();
    }

    @PostMapping("sent")
    public Object setVar(@RequestParam String phone) {
        return authService.sentNum(phone);
    }

    @PostMapping("check")
    public Object checkVar(@RequestParam String phone, @RequestParam String var) {
        return authService.varNum(phone, var);
    }
}
